package com.jiaoyou.baojian.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiaoyou.baojian.R;
import com.wang.avi.AVLoadingIndicatorView;

/**
 * Created by DemonZYM on 17/5/5.
 */

public class LoadingDialog {
    private Dialog mLoadingDialog;
    private AVLoadingIndicatorView mLoadingView;
    private TextView loadingText;

    public LoadingDialog(Context context, String msg) {
        // 首先得到整个View
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_loading, null);
        // 获取整个布局
        RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.dialog_loading_view);
        // 页面中的LoadingView
        mLoadingView = (AVLoadingIndicatorView) view.findViewById(R.id.avLoadingView);
        // 页面中显示文本
        loadingText = (TextView) view.findViewById(R.id.msg);
        // 显示文本
        loadingText.setText(msg);
        // 创建自定义样式的Dialog
        mLoadingDialog = new Dialog(context, R.style.loading_dialog);
        // 设置返回键无效
        mLoadingDialog.setCancelable(false);
        mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
    }

    public void show(){
        mLoadingDialog.show();
        mLoadingView.show();
    }

    public void setMsg(String s){
        loadingText.setText(s);
    }

    public void close(){
        if (mLoadingDialog != null) {
            mLoadingView.hide();
            mLoadingDialog.dismiss();
        }
    }
}